package com.tim.servlet;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tim.pojo.DataResult;
import com.tim.pojo.House;
import com.tim.service.HouseService;
import com.tim.service.Impl.HouseServiceImpl;
import com.tim.util.FileUploadUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 作者：Tim
 * 时间：2021/4/26 20:33
 */
@WebServlet("/house/*")
@MultipartConfig
public class HouseServlet extends BaseServlet{
    HouseService houseService = new HouseServiceImpl();
    /**
     * 分页查询所有
     */
    public DataResult all(HttpServletRequest req, HttpServletResponse resp){
        //1、获取请求参数
        String page = req.getParameter("page");
        String limit = req.getParameter("limit");
        String houseName = req.getParameter("houseName");
        DataResult dataResult = houseService.limit(page,limit,houseName);
        return dataResult;

    }

    /**
     * 删除
     */
    public DataResult del(HttpServletRequest req, HttpServletResponse resp) {
        //1、获取请求参数
        String id = req.getParameter("houseID");
        int count = houseService.deleteHouse(id);
        DataResult dataResult;
        if(count>0){
            dataResult = new DataResult(0,"删除成功");
        }else{
            dataResult = new DataResult(40000,"删除失败");
        }
        return dataResult;
    }
    /**
     * 添加
     */
    public DataResult add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //1、获取请求参数
        String info = req.getParameter("info");
        //将前端传递的json字符串转换成java对象
        ObjectMapper mapper = new ObjectMapper();
        House house = mapper.readValue(info,House.class);
        //2、调用Service层方法
        int count = houseService.addHouse(house);
        DataResult dataResult ;
        if(count>0){
            dataResult = new DataResult(0,"添加成功");
        }else{
            dataResult = new DataResult(40000,"添加失败");
        }
        return dataResult;
    }
    /**
     * 上传图片
     */
    public DataResult upload(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        DataResult dataResult;
        try {
            //调用工具类上传文件
            String sqlPath = FileUploadUtil.upload(request);
            //将地址返回到前端
            dataResult = new DataResult(0,"上传成功",sqlPath);
        } catch (Exception e) {
            e.printStackTrace();
            dataResult = new DataResult(40000,"上传失败");
        }
        return dataResult;
    }

    /**
     * 根据id查询
     */
    public DataResult one(HttpServletRequest request,HttpServletResponse response){
        String id = request.getParameter("houseID");
        House house = houseService.selectOne(id);
        DataResult dataResult;
        if(house==null){
            dataResult=new DataResult(40000,"查无结果");
        }
        dataResult = new DataResult(0,"查询成功",house);
        return dataResult;
    }

    /**
     *  修改编辑
     */
    public DataResult update(HttpServletRequest request, HttpServletResponse response)  throws ServletException, IOException {
        //1、获取请求参数
        String info = request.getParameter("info");
        //将前端传递的json字符串转换成java对象
        ObjectMapper mapper = new ObjectMapper();
        House house = mapper.readValue(info, House.class);
        //2、调用Service层方法
        int count = houseService.updateHouse(house);
        DataResult dataResult ;
        if(count>0){
            dataResult = new DataResult(0,"修改成功");
        }else{
            dataResult = new DataResult(40000,"修改失败");
        }
        return dataResult;
    }
}
